package com.achilles.fragment;


import android.content.Context;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.achilles.bean.DataBean;
import com.achilles.playtask.R;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public abstract class BaseListFragment extends BaseFragment {

    private ListView mListView;
    private ListAdapter mListAdapter;

    @Override
    protected void onViewCreated(View view) {
        mListView = (ListView) view.findViewById(R.id.listView);
        Log.d("BaseListFragment","onViewCreated mListView is null:" + (mListView == null));
        if (mListView == null) {
            throw new IllegalArgumentException("BaseListFragment need to contain listview!");
        }
        mListAdapter = new ListAdapter(getActivity());
        mListView.setAdapter(mListAdapter);
        mListView.setEmptyView(getEmptyView(view));
        mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            private boolean scrollFlag = false;
            private int lastVisibleItemPosition = 0;

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                switch (scrollState) {
                    case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
                        scrollFlag = false;
                        break;
                    case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                        scrollFlag = true;
                        break;
                    case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
                        scrollFlag = true;
                        break;
                    default:
                        scrollFlag = false;
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                                 int totalItemCount) {
                if (scrollFlag) {
                    Log.d("onScroll","firstVisibleItem: " + firstVisibleItem);
                    Log.d("onScroll","lastVisibleItemPosition: " + lastVisibleItemPosition);

                    if (firstVisibleItem > lastVisibleItemPosition) {
                        Log.d("onScroll","onScroll up");
                        onScrollDirectionChanged(true);
                    } else if (firstVisibleItem < lastVisibleItemPosition) {
                        Log.d("onScroll","onScroll down");
                        onScrollDirectionChanged(false);
                    } else {
                        return;
                    }
                    lastVisibleItemPosition = firstVisibleItem;
                }
            }
        });
    }

    class ListAdapter extends BaseAdapter {

        private final List<DataBean> mDataBeanList = new ArrayList<>();

        private final Context mContext;
        public ListAdapter(Context context) {
            mContext = context;
        }

        @Override
        public int getCount() {
            return mDataBeanList.size();
        }

        @Override
        public Object getItem(int position) {
            return mDataBeanList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            DataBean dataBean = (DataBean) getItem(position);
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(getListItemLayout(), parent,
                        false);
            }
            return onGetView(position,getCount(),dataBean, convertView);
        }

        public void notifyChanged(List<DataBean> dataBeanList) {
            mDataBeanList.clear();
            mDataBeanList.addAll(dataBeanList);
            this.notifyDataSetChanged();
        }
    }

    protected ListView getListView() {
        return mListView;
    }

    protected void notifyListDataChanged(List<DataBean> dataBeenList) {
        mListAdapter.notifyChanged(dataBeenList);
    }

    public abstract void updateList();

    protected abstract View onGetView(int position, int size, DataBean dataBean, View convertView);

    protected abstract int getListItemLayout();

    protected abstract View getEmptyView(View view);

    protected abstract void onScrollDirectionChanged(boolean isScrollUp);

}
